﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Restaurant.DTO;
using Restaurant.BUS;

namespace Restaurant
{
    public partial class frmNVQLKho : Form
    {
        private KhoService KhoService = new KhoService();
        private NhaHangService NhaHangService = new NhaHangService();

        public frmNVQLKho()
        {
            InitializeComponent();
        }

        private void btnXuat_Click(object sender, EventArgs e)
        {
            if (lv_Kho.FocusedItem != null)
            {
                int maKho = int.Parse(lv_Kho.FocusedItem.SubItems[0].Text);
                string tenKho = lv_Kho.FocusedItem.SubItems[1].Text;
                //MessageBox.Show("Bạn đã chọn Kho hàng: ",maKho.ToString());
                Form XK = new frmXuatKho(maKho);
                XK.Text = "Xuất kho hàng: " + tenKho;
                XK.Show();
            }
            else
            {
                MessageBox.Show("Bạn chưa chọn kho hàng");
            }
        }
        private void LoadNhaHang()
        {
            NhaHangService NCC = new NhaHangService();
            cmbNH.DisplayMember = "TenNH";
            cmbNH.ValueMember = "MaNH";
            //cmbNH.Items.Add("Tất cả");
            cmbNH.DataSource = NCC.LoadNH();
        }
        public void LoadThongTinKho(DataTable dbtab)
        {
            lv_Kho.Items.Clear();
            for (int i = 0; i < dbtab.Rows.Count; i++)
            {
                DataRow dbrow = dbtab.Rows[i];
                ListViewItem liv = new ListViewItem();
                liv.Text = dbrow[0].ToString();
                liv.SubItems.Add(dbrow[1].ToString());
                liv.SubItems.Add(dbrow[2].ToString());
                liv.SubItems.Add(dbrow[5].ToString());
                lv_Kho.Items.Add(liv);
            }
        }

        private void frmNVQLKho_Load(object sender, EventArgs e)
        {
            DataTable dbtab = KhoService.LayKho();
            LoadThongTinKho(dbtab);
            LoadNhaHang();
        }

        private void cmbNH_SelectedIndexChanged(object sender, EventArgs e)
        {
            int maNH = int.Parse(cmbNH.SelectedValue.ToString());
            DataTable dbtab = KhoService.LayKhoTheoNH(maNH);
            LoadThongTinKho(dbtab);
        }

        private void btnNhapHang_Click(object sender, EventArgs e)
        {
            if (lv_Kho.FocusedItem != null)
            {
                int maKho = int.Parse(lv_Kho.FocusedItem.SubItems[0].Text);
                string tenKho = lv_Kho.FocusedItem.SubItems[1].Text;
                //MessageBox.Show("Bạn đã chọn Kho hàng: ",maKho.ToString());
                Form NH = new frmNhapHang(maKho);
                NH.Text = "Nhập hàng kho hàng: " + tenKho;
                NH.Show();
            }
            else
            {
                MessageBox.Show("Bạn chưa chọn kho hàng");
            }
        }

        private void btn_CapNhat_Click(object sender, EventArgs e)
        {
            DataTable dbtab = KhoService.LayKho();
            LoadThongTinKho(dbtab);
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            Form ctk = new frmChiTietKho();
            ctk.Show();
        }
    }
}
